require.config({
    paths: {
        handlebars: "lib/handlebars",
        text: "lib/text",
        hbs: "lib/hbs",
		jQuery: "lib/jquery.min",
		'jQuery-ui': "lib/jquery-ui.min",	
		jsencrypt: "lib/jsencrypt",
		fastclick: "lib/fastclick",		
		moment		: "lib/moment.min",
		calendar: "lib/fullcalendar.min",
		lang	: "lib/zh-cn",
		highchart	: "lib/highcharts.src"
    },
    shim: {
        handlebars: {
            exports: "Handlebars"
        },
		jQuery:		{
			exports: "$"
		}
		,
		jsencrypt:  {
			exports: "jsencrypt"
		}
		,
		monent	:   {
			deps:['jQuery']
		}
		,
		calendar:{			
			exports: "fullCalendar"
		}
		,
		highchart:{
			deps:['jQuery'],
			exports:'jQuery.fn.highcharts'
		}
		
    },
	nodeRequire: require
});
define('app', ['js/router','js/utils','fastclick','jQuery','jsencrypt','js/init','js/util/toast',
	'text!app/login.html','text!app/config/view-config.html','text!app/risk/view-risk.html'
	,'text!app/signature/view-signature.html','text!app/approve/view-approve.html'], 
	function(Router,Utils,fastclick,$,jsencrypt,base,toast,
	loginpage,configpage,riskpage,signaturepage,approvepage) {

	'use strict';	
	
	var $$ = Dom7;
	var pk;

	var lo = base.base;

	var userinfo ;

	var versionName ;

	var urlConfig = {
		login: '/biz/login.action'
	}	
	
	var myApp = {
		f7: null,						
		router	: Router,
		utils	: Utils ,
		exit	: exitApp
	}
	
	var private_method = {

		getApp: function(){
			return myApp;
		}
		
		,
		
		getCurrentVersion : function(){
			return versionName;
		}
		
		,
		// Application Constructor
		initialize: function() {
			document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
		},

		// deviceready Event Handler
		//
		// Bind any cordova events here. Common events are:
		// 'pause', 'resume', etc.
		onDeviceReady: function() {

			fastclick.attach(document.body);
			this.receivedEvent('deviceready');
		},

		// Update DOM on a Received Event
		receivedEvent: function(id) {
			//var parentElement = document.getElementById(id);
			//var listeningElement = parentElement.querySelector('.listening');
			//var receivedElement = parentElement.querySelector('.received');
			
			//listeningElement.setAttribute('style', 'display:none;');
			//receivedElement.setAttribute('style', 'display:block;');			
			
			private_method.viewInit();
			if(console && console.log){
				console.log('Received Event: ' + id);
			}
		},
		
		getUser:function(){
			return userinfo;
		}
		,
		
		getUserid:function(){
			if(userinfo && userinfo.userid){
				return userinfo.userid;
			}
		}
		,

		setUser:function(u){
			userinfo = u
		}
		,
		viewInit:function(){			
			
			
			var loginHtml = $('.view-login').html(loginpage);
			$('#view-approve').html(approvepage);
			$('#view-risk').html(riskpage);
			$('#view-signature').html(signaturepage);
			$('#view-config').html(configpage);


			//$('.view-patient').html(patientpage);
			Router.init();
			var f7 = new Framework7({
				modalTitle			: '综合管理平台',
				dynamicNavbar		: true,
				animateNavBackIcon	: true,
				material			: true
			});
			
			
			
			if(AppVersion && AppVersion.version){
				versionName = AppVersion.version;
			}else{
				versionName = '1.0.0';
			}

			loginHtml.find('.version-name').html(versionName);
			

			var _tmpApp = {
				f7: f7,						
				router	: Router,
				setUser	: private_method.setUser,
				getUser : private_method.getUser,
				getUserid: private_method.getUserid,
				getCurrentVersion:private_method.getCurrentVersion,
				views	: {}
			};
			var mainView = f7.addView('.view-main');
			
			_tmpApp.views['mainView'] = mainView;
			
			
			_tmpApp.views['approve'] = f7.addView('#view-approve',{name:'approve'});
			
			
			_tmpApp.views['risk'] = f7.addView(
				'#view-risk',
				{name:'risk',dynamicNavbar: true,domCache: true});

			
			_tmpApp.views['signature'] = f7.addView(
				'#view-signature',
				{name:'signature',dynamicNavbar: true,domCache: true});

			
			_tmpApp.views['config'] = f7.addView(
				'#view-config',
				{name:'config',dynamicNavbar: true,domCache: true});
			
			

			_tmpApp.mainView = mainView;
			f7.hideNavbar();

			$.extend(myApp,_tmpApp);
			
			

			document.addEventListener("backbutton", quit, false);
		}
	}

	//退出系统
	function quit(){
		toast.show({msg:'再点击一次退出!'});  
                document.removeEventListener("backbutton", quit, false); // 注销返回键  
                document.addEventListener("backbutton", exitApp, false);//绑定退出事件  
                // 3秒后重新注册  
                var intervalID = window.setInterval(function() {  
                    window.clearInterval(intervalID);  
                    document.removeEventListener("backbutton", exitApp, false); // 注销返回键  
                    document.addEventListener("backbutton", quit, false); // 返回键  
                }, 3000);  
	}

	function exitApp(){  
		navigator.app.exitApp();  
	} 

	private_method.initialize();

	return {getApp:private_method.getApp};

});